﻿namespace com.crownpack.InitiativesDao
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    using com.crownpack;
    using com.crownpack.Extensions.SharePointExtensions;
    using com.crownpack.Extensions.StringExtensions;
    using com.crownpack.Extensions.SymbolExtensions;
    using com.crownpack.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    public class InitiativesMasterDao : BaseDao
    {
        #region Fields

        public const string ListName = "initiatives";

        List<string> _BaseFields;

        #endregion Fields

        #region Constructors

        public InitiativesMasterDao( )
            : base(ListName)
        {
            CurrentWebUrl = string.Format( "{0}/i2e", SPContext.Current.Site.RootWeb.Url );
        }

        public InitiativesMasterDao( string currentWebUrl )
            : base(ListName)
        {
            using ( SPSite spSite = new SPSite( currentWebUrl ) )
            {
                CurrentWebUrl = string.Format( "{0}/i2e", spSite.RootWeb.Url );
            }
        }

        #endregion Constructors

        #region Properties

        List<string> BaseFields
        {
            get
            {
                if ( _BaseFields == null )
                {
                    _BaseFields = new List<string>( );
                    _BaseFields.AddRange( new string[ ] {
                        "Title",
                    }.ToList( ) );
                }
                return _BaseFields;
            }
            set
            {
                _BaseFields = value;
            }
        }

        #endregion Properties

        #region Methods

        public void Delete( SPListItem value )
        {
            using ( SPSite spSite = new SPSite( CurrentWebUrl ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPListItem item = get( value.UniqueId );
                    if ( item != null )
                        item.Delete( );
                }
            }
        }

        public void Upsert( SPListItem value )
        {
            using ( SPSite spSite = new SPSite( CurrentWebUrl ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPListItem item = get( value.UniqueId );
                    if ( item == null )
                    {
                        SPList spList = spWeb.Lists[ ListTitle ];
                        item = spList.AddItem( );
                    }

                    List<Guid> source = new List<Guid>( );
                    foreach ( SPField spField in value.Fields )
                        if ( spField.CanBeDeleted || BaseFields.Contains( spField.InternalName ) )
                            source.Add( spField.Id );
                    List<Guid> target = new List<Guid>( );
                    foreach ( SPField spField in item.Fields )
                        if ( spField.CanBeDeleted || BaseFields.Contains( spField.InternalName ) )
                            target.Add( spField.Id );
                    bool ok = false;
                    foreach ( Guid id in source )
                    {
                        if ( target.Contains( id ) )
                        {
                            item[ id ] = value[ id ];
                            ok = true;
                        }
                    }
                    if ( ok )
                    {
                        item[ "Content Type" ] = ContentTypeName.RollupInitiative;
                        item.UpdateOverwriteVersion( );
                    }
                }
            }
        }

        SPListItem get( Guid uniqueId )
        {
            List<SPListItem> l = getItems( Query.Get( Query.ByUniqueId, uniqueId.ToString( ) ) );
            if ( l.Count > 0 )
                return l[ 0 ];
            else
                return null;
        }

        List<SPListItem> getItems( string query )
        {
            using ( SPSite spSite = new SPSite( CurrentWebUrl ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPList spList = spWeb.Lists[ ListTitle ];
                    SPQuery spQuery = new SPQuery
                    {
                        Query = query
                    };
                    SPListItemCollection spListItemCollection = spList.GetItems( spQuery );
                    return spListItemCollection.Count > 0 ?
                        spListItemCollection.Cast<SPListItem>( ).ToList( ) :
                        new List<SPListItem>( );
                }
            }
        }

        #endregion Methods

        #region Nested Types

        class Query
        {
            #region Fields

            public const string ByUniqueId = "<Where><Eq><FieldRef Name='cpInitiativeItemID' /><Value Type='Text'>{0}</Value></Eq></Where>";

            #endregion Fields

            #region Methods

            public static string Get( string query, params object[ ] args )
            {
                return string.Format( query, args );
            }

            #endregion Methods
        }

        #endregion Nested Types
    }
}